﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSearch.Models;
using Golem.SearchDatabase;
using WebSearch.Helpers;

namespace WebSearch.Controllers
{
    public class SearchController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(SearchModel searchModel)
        {
            if (searchModel.FulltextQuery == null || searchModel.FulltextQuery == String.Empty) {
                return RedirectToAction("Index");
            }

            MSSQLDatabase db = new MSSQLDatabase("localhost", "webSearch", "search", "search");
            QueryBuilder qb = new QueryBuilder(searchModel.QueryMode);
            string query = qb.BuildQuery(searchModel.FulltextQuery);
            ViewData["query"] = query;
            searchModel.Results = db.DoFulltextSearch(query, searchModel.QueryTypes);
            searchModel.ResultView = "ResultMachine";

            return View("Results", searchModel);
        }
    }
}
